package com.kly.pay.db.dao.impl;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.DateUtil;
import com.google.common.collect.Maps;
import com.kly.enums.PayStatusEnum;
import com.kly.pay.db.dao.POrderDao;
import com.kly.pay.db.entity.POrderEntity;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
* User: linqu
* Date: 2023-06-06
* Time: 20:29:09.685
*/

@Repository
public class POrderDaoImpl extends AbstractBaseMapper<POrderEntity> implements POrderDao {

    @Override
    public POrderEntity queryByOrderNoAndPayType(String orderNo, String payType) {
        final HashMap<Object, Object> params = Maps.newHashMap();
        params.put("orderNo", orderNo);
        params.put("payType", payType);
        params.put("deleted", 0);
        return this.getSqlSession().selectOne(this.getStatement("queryByOrderNoAndPayType"), params);
    }

    @Override
    public POrderEntity queryByPayOrderNoAndPayType(String payOrderNo, String payType) {
        final HashMap<Object, Object> params = Maps.newHashMap();
        params.put("payOrderNo", payOrderNo);
        params.put("payType", payType);
        params.put("deleted", 0);
        return this.getSqlSession().selectOne(this.getStatement("queryByOrderNoAndPayType"), params);
    }

    @Override
    public List<POrderEntity> queryProcessingRecords(Integer payType) {
        final HashMap<Object, Object> params = Maps.newHashMap();
        params.put("payType", payType);
        params.put("status", PayStatusEnum.PROCESSING.getCode());
        params.put("deleted", 0);
        return this.getSqlSession().selectList(this.getStatement("queryByOrderNoAndPayType"), params);
    }

    @Override
    public POrderEntity queryByPayOrderNo(String payOrderNo) {
        final HashMap<Object, Object> params = Maps.newHashMap();
        params.put("payOrderNo", payOrderNo);
        params.put("deleted", 0);
        return this.getSqlSession().selectOne(this.getStatement("queryByPayOrderNo"), params);
    }

    @Override
    public int updateOrderStatus(String payOrderNo, Integer payType, Integer status) {

        POrderEntity record = new POrderEntity();
        record.setPayOrderNo(payOrderNo);
        record.setPayType(payType);
        record.setStatus(status);
        return this.getSqlSession().update(this.getStatement("updateOrderStatus"), record);
    }

    @Override
    public int repaySuccess(String payOrderNo, Integer payType) {
        POrderEntity record = new POrderEntity();
        record.setPayOrderNo(payOrderNo);
        record.setPayType(payType);
        record.setStatus(PayStatusEnum.PAY_SUCCESS.getCode());
        record.setAccountingDate(DateUtil.getDate("yyyy-MM-dd HH:mm:ss"));
        return this.getSqlSession().update(this.getStatement("repaySuccess"), record);
    }
}